package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendPayMethodDAO;
import org.jsola.hr.dto.AttendPayMethodAddDTO;
import org.jsola.hr.dto.AttendPayMethodUpdateDTO;
import org.jsola.hr.entity.AttendPayMethodDO;
import org.jsola.hr.query.AttendPayMethodQuery;
import org.jsola.hr.service.IAttendPayMethodService;
import org.jsola.hr.vo.AttendPayMethodListVO;
import org.jsola.hr.vo.AttendPayMethodVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendPayMethodServiceImpl")
public class AttendPayMethodServiceImpl implements IAttendPayMethodService {

    @Autowired
    private IAttendPayMethodDAO attendPayMethodDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendPayMethodVO save(AttendPayMethodAddDTO attendPayMethodAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendPayMethodDO attendPayMethodDO = attendPayMethodAddDTO.to(AttendPayMethodDO.class);
        // 保存
        attendPayMethodDO = save(attendPayMethodDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendPayMethodDO.to(AttendPayMethodVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendPayMethodUpdateDTO attendPayMethodUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendPayMethodDO attendPayMethodDO = attendPayMethodUpdateDTO.to(AttendPayMethodDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendPayMethodDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendPayMethodIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendPayMethodIds);
    }


    @Override
    public AttendPayMethodVO selectById(String attendPayMethodId, String siteId) {
        AttendPayMethodDO attendPayMethodDO = selectDOById(attendPayMethodId, siteId);
        if (attendPayMethodDO == null) {
            return null;
        }
        return attendPayMethodDO.to(AttendPayMethodVO.class);
    }

    @Override
    public List<AttendPayMethodListVO> select(AttendPayMethodQuery attendPayMethodQuery, String siteId) {
        List<AttendPayMethodDO> attendPayMethodDOList = selectDO(attendPayMethodQuery, siteId);
        if(CollectionUtils.isEmpty(attendPayMethodDOList)) {
            return attendPayMethodDOList == null ? null : new ArrayList<>();
        }
        return attendPayMethodDOList.stream()
                .map(attendPayMethodDO -> attendPayMethodDO.to(AttendPayMethodListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendPayMethodQuery attendPayMethodQuery, String siteId) {
        Example example = buildExample(attendPayMethodQuery,siteId);
        return attendPayMethodDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendPayMethodListVO> selectPage(AttendPayMethodQuery attendPayMethodQuery, String siteId) {
        Example example = buildExample(attendPayMethodQuery,siteId);
        Page<AttendPayMethodDO> page = attendPayMethodDAO.selectPageByExample(example,
                attendPayMethodQuery.getPageNo(),
                attendPayMethodQuery.getPageSize());

        return page.to(AttendPayMethodListVO.class);
    }

    @Override
    public AttendPayMethodDO selectDOById(String attendPayMethodId, String siteId) {
        return listById(attendPayMethodId, siteId);
    }

    @Override
    public List<AttendPayMethodDO> selectDO(AttendPayMethodQuery attendPayMethodQuery, String siteId) {
        Example example = buildExample(attendPayMethodQuery,siteId);
        return attendPayMethodDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param attendPayMethodQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendPayMethodQuery attendPayMethodQuery, String siteId) {
        Example example = new Example(AttendPayMethodDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendPayMethodQuery.getAttendPlanId() != null) {
            example.and().andLike("attendPlanId", "%" + attendPayMethodQuery.getAttendPlanId() + "%");
        }
        if (attendPayMethodQuery.getPayMethod() != null) {
            example.and().andEqualTo("payMethod", attendPayMethodQuery.getPayMethod());
        }
        if (attendPayMethodQuery.getSpecificWay() != null) {
            example.and().andEqualTo("specificWay", attendPayMethodQuery.getSpecificWay());
        }
        if (attendPayMethodQuery.getHours() != null) {
            example.and().andEqualTo("hours", attendPayMethodQuery.getHours());
        }
        if (attendPayMethodQuery.getDays() != null) {
            example.and().andEqualTo("days", attendPayMethodQuery.getDays());
        }
        if (attendPayMethodQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", attendPayMethodQuery.getCompanyId());
        }
        if (attendPayMethodQuery.getGmtCreateStart() != null &&
                attendPayMethodQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendPayMethodQuery.getGmtCreateStart(),
                    attendPayMethodQuery.getGmtCreateEnd());
        }
        if (attendPayMethodQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendPayMethodQuery.getCreateUserId() + "%");
        }
        if (attendPayMethodQuery.getGmtModifiedStart() != null &&
                attendPayMethodQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendPayMethodQuery.getGmtModifiedStart(),
                    attendPayMethodQuery.getGmtModifiedEnd());
        }
        if (attendPayMethodQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendPayMethodQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendPayMethodQuery.getOrders());
        return example;
    }
}




